package com.ruoyi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.HotelOrder;
import com.ruoyi.service.LogService;
import com.ruoyi.service.OrderService;
import com.ruoyi.service.OrderTwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 订单模块,用于提供给同程回调
 * 2024-10-24
 */
@RestController
@RequestMapping("/hotel/order")
public class OrderController extends BaseController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderTwoService orderTwoService;

    @Autowired
    LogService logService;

    /**
     * 提供给同程用于可订、创建订单、轮询、取消订单
     *
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/isOrder")
    public String isOrder(@RequestParam Map map) throws Exception {
        String ru = map.get("Message").toString();
        return orderService.isOrder(ru);
    }

    /**
     * 提供给同程用于特殊退款场景
     * @param jsonObject
     * @return
     */
    @PostMapping("/isOrderSpecial")
    public JSONObject isOrderSpecial(@RequestBody JSONObject jsonObject){
        return orderTwoService.isOrderSpecial(jsonObject);
    }

    /**
     * 获取订单数据
     *
     * @param map
     * @return
     */
    @GetMapping("/getOrderList")
    public TableDataInfo getOrderList(@RequestParam Map map) {
        startPage();
        List<Map> list = orderTwoService.getOrderList(map);
        return getDataTable(list);
    }

    /**
     * 处理新订订单
     * @param jsonObject
     * @return
     */
    @PostMapping("/orderHandlePost")
    public AjaxResult orderHandlePost(@RequestBody JSONObject jsonObject) {
        return toAjax(orderTwoService.orderHandlePost(jsonObject));
    }

    /**
     * 处理取消订单
     * @param jsonObject
     * @return
     */
    @PostMapping("/updateOrderStatusPost")
    public AjaxResult updateOrderStatusPost(@RequestBody JSONObject jsonObject) {
        return toAjax(orderTwoService.updateOrderStatusPost(jsonObject));
    }

    /**
     * 获取新订订单未提示的数据
     * @return
     */
    @GetMapping("/getOrderByNoRemind")
    public AjaxResult getOrderByNoRemind() {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            return success(orderTwoService.getOrderByNoRemind(loginUser.getUserId()));
        } catch (Exception e) {
            System.out.println("未登录状态");
            return success();
        }
    }

    /**
     * 修改发单状态
     * @param map
     * @return
     */
    @PostMapping("/postCopyAndSend")
    public AjaxResult postCopyAndSend(@RequestBody Map map) {
        return success(orderTwoService.postCopyAndSend(map));
    }

    /**
     * 修改订单专用采购价
     * @param jsonObject
     * @return
     */
    @PostMapping("/postUpdateOrderPrice")
    public AjaxResult postUpdateOrderPrice(@RequestBody JSONObject jsonObject) {
        return toAjax(orderTwoService.postUpdateOrderPrice(jsonObject));
    }

    /**
     * 导出订单excel
     * @param response
     * @param map
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestParam Map map)
    {
        List<HotelOrder> list = orderTwoService.getOrderListExport(map);
        ExcelUtil<HotelOrder> util = new ExcelUtil<HotelOrder>(HotelOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    @GetMapping("/yjcl")
    public void yjcl(){
        orderService.yjcl();
    }

    @GetMapping("/insertPrice")
    public void insertPrice(){
        orderService.insertPrice();
    }
}
